<template>
	<view class="colle page bgf6" >

		<view class="bgff flex flexC flexB px20 py16 borb  fs16">
			<text>门店头像</text>
			<view class="flex flexC" @click="chooseImage">
				<u-avatar :src="data.logo" mode="aspectFill"></u-avatar>
				<text class="arrowR dinline mt2"></text>
			</view>
		</view>

		<view class="bgff flex flexC flexB px20 py16 borb fs16">
			<text>手机号</text>
			<view class="flex flexC">

				<view>
					<text class="c8e fs14 px8">{{data.mobile}}</text>
				</view>
				<text class="arrowR dinline mt2"></text>
			</view>
		</view>

		<view class="bgff flex flexC flexB px20 py16 borb fs16" @click="openLocation()">
			<text class="w28">地址</text>
			<view class="flex flexC">
				<view>
					<text class="c8e fs14 px8">{{data.address}}</text>
				</view>
				<text class=" dinline mt2"></text>
			</view>
		</view>


		<view class="bgff flex flexC flexB px20 mt14 py16 borb fs16" >
			<text>店铺编号</text>
			<view class="flex flexC">

				<view>
					<text class="c8e fs14 px8">{{data.store_no}}</text>
				</view>
				<text class="dinline mt2"></text>
			</view>
		</view>


		<button @click="logout" hover-class="none" class="r40 bg41 m40 fs16 py16">退出登录</button>

	</view>
</template>

<script>
	export default {
		data() {
			return {

				data: null,
				frontUrl: '',
				frontPath: '',
				versionCode:'',
				lat:'',
				lng:'',
				address:'',
				province:'',
				city:'',
				area:''
			};
		},
		onLoad() {
		
			this.getDetail();
		},
		onShow() {

		},
		methods: {
			
		
			

			chooseImage() {

				let that = this;

				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ["camera", "album"], //从相册选择
					success: function(res) {
						console.log(JSON.stringify(res.tempFilePaths));

						uni.showLoading({
							title: "上传中"
						})
						console.info("===1===")
						let images = res.tempFilePaths; //非APP平台不支持自定义压缩,暂时没有处理,可通过uni-app上传组件的sizeType属性压缩
						for (var i = 0; i < images.length; i++) {
							let tImg = images[i];


							that.upload(tImg);
						}
					},
					fail(res) {
						console.info(res);
						uni.hideLoading();
					}
				});
			},

			upload(img) {

				let that = this;
				console.info("===2===" + img)
				uni.uploadFile({
					url: this.$uploadUrl, //仅为示例，非真实的接口地址
					filePath: img,
					name: 'file',
					success: (uploadFileRes) => {

						uni.hideLoading();
						var res = uploadFileRes.data;

						// 根据接口具体返回格式   赋值具体对应url
						var url = JSON.parse(res).data.fullurl;

						var uploadUrl = JSON.parse(res).data.url;

						that.frontPath = uploadUrl;
						that.frontUrl = url;
						that.data.logo = url;
						that.updateAvatar();

					},
					fail() {
						uni.hideLoading();
					}
				});


			},

			logout() {


				let param = {}
				let that = this;
				this.$api.logout(param).then(res => {
					console.info(res);
					if (res.code == 1) {

						uni.removeStorageSync("token");
						uni.removeStorageSync("isVerify");
						uni.navigateTo({
							url: "../signUp/login"
						})
					} else {

					}
				})
			},


			updateAvatar() {
				let param = {
					logo: this.frontPath
				}
				let that = this;
				this.$api.shopUpdate(param).then(res => {
					console.info(res);
					if (res.code == 1) {
						uni.showToast({
							title: "修改成功",
							icon: 'success'
						});
					} else {

					}
				})

			},


			call() {
				var mobile = this.data.ref_mobile2;
				uni.makePhoneCall({
					phoneNumber: mobile
				})
			},

			updateAddress(){
				
				let param = {
					lat: this.lat,
					lng:this.lng,
					address:this.address
				}
				let that = this;
				this.$api.shopUpdate(param).then(res => {
					console.info(res);
					if (res.code == 1) {
						that.data.address = that.address;
						uni.showToast({
							title: "修改成功",
							icon: 'success'
						});
					} else {
				
					}
				})
			},
			openLocation(){
				let that = this;
				uni.chooseLocation({
						
					fail:function(res){
						console.info(res);
					},
					 success: function (res) {
						 console.info(res)
						 that.lat = res.latitude;
						 that.lng = res.longitude;
						 
						 let regex = "(?<province>[^省]+省|.+自治区)(?<city>[^自治州]+自治州|[^市]+市|[^盟]+盟|[^地区]+地区|.+区划)(?<county>[^市]+市|[^县]+县|[^旗]+旗|.+区)?(?<town>[^区]+区|.+镇)?(?<village>.*)"
						 let newAddress = res.address.match(regex);
						 let group = newAddress.groups;
						 
						
						 that.province= group.province;
						 that.city= group.city;
						  if(group.county != null){
							 that.area= group.county;
						 }
						 if(group.town != null){
						 	that.area= group.county;
						 }
						 that.address = that.province +" "+ that.city+" "+that.area+group.village;
						 that.updateAddress();
					     console.log('位置名称：' + res.name);
					     console.log('详细地址：' + res.address);
					     console.log('纬度：' + res.latitude);
					     console.log('经度：' + res.longitude);  
					    }
				})
			},

			getDetail() {

				let param = {}
				let that = this;
				this.$api.shopInfo(param).then(res => {
					console.info(res);
					if (res.code == 1) {
						that.data = res.data;
						if (that.data.logo == '') {
							that.data.logo = "/static/logo.jpg"
						}
					} else {

					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.colle {
		.w20 {
			width: 80rpx;
			height: 80rpx;
		}

		.w28 {
			width: 106rpx;
		}

		.maxH {
			height: 60vh;
		}
	}
</style>
